package com.douziit.meilianoa.base.utils;

import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by dev31 on 2018/7/2.
 */
public class RequestParameterMap {

	/****
	 * 获取请求的参数信息转成map
	 * 
	 * @param request
	 * @return
	 */
	public static Map<String, String> parseRequestParameterMap(HttpServletRequest request) {
		Map<String, String> resultMap = new HashMap<>();

		Map<String, String[]> parameterMap = request.getParameterMap();
		for (String strings : parameterMap.keySet()) {
			resultMap.put(strings, parameterMap.get(strings)[0]);
		}
		return resultMap;

	}

	/****
	 * 把xml转成map
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> parseRequestParameterXML(HttpServletRequest request) throws Exception {
		Map<String, String> resultMap = new HashMap<>();

		InputStream is = request.getInputStream();
		String result = IOUtils.toString(is, "UTF-8");
		// 解析xml
		resultMap = MapXmlUtil.xml2Map(result, "xml");

		return resultMap;

	}

}
